package com.five.dao.impl.backstage;

import com.five.dao.backstage.StaffDao;
import com.five.entity.backstage.TStaff;
import com.five.util.DBUtil;
import com.five.util.Page;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @Author: 王登峰
 * @Description:
 * @Date: 2020/11/23 14:57
 */

//放入bean里面
@Component
//但是dao层一般用
//@Repository
public class StaffDaoImpl implements StaffDao {
//模糊查询
    @Override
    public List<Map> selectStaff(Page pageInfo,TStaff tStaff) {
        StringBuffer sql = new StringBuffer();
        sql.append("select a.id,b.id deptid,c.id roleid,a.name nstaff,a.rzdate,a.lzdate,a.address,a.status,b.name ndept,c.name nrole\n" +
                "from t_staff a\n" +
                "left join t_dept b on a.deptid = b.id\n" +
                "left join t_role c on a.roleid = c.id " +
                "where 1=1");
        if(tStaff.getSnstaff()!= null && tStaff.getSnstaff()!= ""){
            sql.append(" and a.name like '%"+tStaff.getSnstaff()+"%'");
        }
        if(tStaff.getSndept()!= null && tStaff.getSndept()!= ""){
            sql.append(" and b.name like '%"+tStaff.getSndept()+"%'");
        }
        sql.append(" order by a.id limit ?,?");
        String s = sql.toString();
        //System.out.println(s);
        int pagesize = pageInfo.getLimit();
        int star = (pageInfo.getPage()-1)*pagesize;
        return DBUtil.queryMap(s,star,pagesize);
    }

    @Override
    public List<Map> selectDept() {
        String sql = "select * from t_dept where status=0";
        return DBUtil.queryMap(sql);
    }

    @Override
    public List<Map> selectRole(Integer id) {
        String sql = "select * from t_role where status=0 and deptid=?";
        return DBUtil.queryMap(sql,id);
    }

    @Override
    public List<Map> selectRoleId(Integer id) {
        String sql="select * from t_role where id=?";
        return DBUtil.queryMap(sql,id);
    }
//添加
    @Override
    public int addStaff(TStaff staff) {
        if(staff.getId() == null) {
            String sql = "insert into t_staff values(null,?,?,null,?,'https://yuyongli.oss-cn-beijing.aliyuncs.com/0aa47c7f-3f0b-4e3f-9100-5457b422ef88.jpeg',?,?,0)";
            return DBUtil.manage(sql, staff.getNstaff(), staff.getRzdate(), staff.getAddress(), staff.getRoleid(), staff.getDeptid());
        }else{
            String sql = "update t_staff set name = ?,rzdate = ?,address = ?,roleid = ?,deptid = ? where id =?";
            return DBUtil.manage(sql,staff.getNstaff(),staff.getRzdate(),staff.getAddress(),staff.getRoleid(),staff.getDeptid(),staff.getId());
        }
    }
//修改离职
    @Override
    public int liZhi(TStaff staff) {
        String sql = "update t_staff set lzdate = ?,status = 1 where id = ?";
        return DBUtil.manage(sql,staff.getLzdate(),staff.getId());
    }
//分页
    @Override
    public int count(TStaff tStaff) {
        StringBuffer sql = new StringBuffer("select count(*) a\n" +
                "from t_staff a\n" +
                "left join t_dept b on a.deptid = b.id\n" +
                "left join t_role c on a.roleid = c.id " +
                "where 1=1");
        if(tStaff.getSnstaff()!= null && tStaff.getSnstaff()!= ""){
            sql.append(" and a.name like '%"+tStaff.getSnstaff()+"%'");
        }
        if(tStaff.getSndept()!= null && tStaff.getSndept()!= ""){
            sql.append(" and b.name like '%"+tStaff.getSndept()+"%'");
        }
        String s = sql.toString();
        List<Map> list = DBUtil.queryMap(s);
        String a = list.get(0).get("a") + "";
        int i = Integer.parseInt(a);
        return i;
    }

    @Override
    public int addStaffpwd(String name, String id) {
        String sql = "insert into t_staff_pwd values(null,?,123123,?)";
        return DBUtil.manage(sql,name,id);
    }

    @Override
    public int upadetaffname(String name, String id) {
        String sql = "update t_staff_pwd set name=? where staffid=?";
        return DBUtil.manage(sql,name,id);
    }

    @Override
    public String selectID(String name) {
        String sql = "select id from t_staff where name = ?";
        return DBUtil.queryMap(sql,name).get(0).get("id")+"";
    }



}
